import { defineStore } from 'pinia';
import { getAllBooks, getBookById, createBook, updateBook, deleteBook, searchBooks, getBooksByCategory } from '@/api/book';

export const useBookStore = defineStore('book', {
  state: () => ({
    books: [],
    currentBook: null,
    total: 0,
    loading: false,
    categories: ['计算机/编程', '计算机/前端', '计算机/云计算', '计算机/Java', '计算机/数据库', '计算机/数据分析', '计算机/架构', '文学', '历史', '科学', '艺术', '其他']
  }),
  
  actions: {
    // 获取所有图书（分页）
    async fetchBooks(page = 1, size = 10, sort = 'id') {
      this.loading = true;
      try {
        const response = await getAllBooks({ page, size, sort });
        console.log('图书API响应:', response);
        const data = response.data || response; // 兼容不同的响应格式
        this.books = data.results || [];
        this.total = data.count || 0;
        return data;
      } catch (error) {
        console.error('获取图书列表失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 根据ID获取图书
    async fetchBookById(id) {
      this.loading = true;
      try {
        const response = await getBookById(id);
        console.log('获取图书详情API响应:', response);
        const book = response.data || response;
        this.currentBook = book;
        return book;
      } catch (error) {
        console.error('获取图书详情失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 创建图书
    async addBook(bookData) {
      this.loading = true;
      try {
        const result = await createBook(bookData);
        return result;
      } catch (error) {
        console.error('创建图书失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 更新图书
    async editBook(id, bookData) {
      this.loading = true;
      try {
        const result = await updateBook(id, bookData);
        return result;
      } catch (error) {
        console.error('更新图书失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 删除图书
    async removeBook(id) {
      this.loading = true;
      try {
        await deleteBook(id);
        // 删除成功后更新列表
        this.books = this.books.filter(book => book.id !== id);
      } catch (error) {
        console.error('删除图书失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 根据标题搜索图书
    async searchByTitle(title) {
      this.loading = true;
      try {
        const books = await searchBooks(title);
        return books;
      } catch (error) {
        console.error('搜索图书失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    },
    
    // 根据分类获取图书
    async fetchBooksByCategory(category) {
      this.loading = true;
      try {
        const books = await getBooksByCategory(category);
        return books;
      } catch (error) {
        console.error('获取分类图书失败:', error);
        throw error;
      } finally {
        this.loading = false;
      }
    }
  }
});
